#include "syscall.h"

int testLock0, testLock1, testCV, testMonitor;

void thread0()
{
	Print("Forked thread 0\n", sizeof("Forked thread 0\n"), -1, -1);
	AcquireLock(testLock0);
	Print("Thread0 lock acquired, now setting monitor to 1\n",sizeof("Thread0 lock acquired, now setting monitor to 1\n"),-1,-1);
	testMonitor = 1;
	Print("Thread0 now waiting\n", sizeof("Thread0 now waiting\n"),-1,-1);
	WaitCondition(testCV, testLock0);
	Print("Thread0 woken up, now releasing lock\n",sizeof("Thread0 woken up, now releasing lock\n"),-1,-1);
	ReleaseLock(testLock0);
	
	Exit(1);
}

void thread1()
{
	Print("Forked thread 1\n", sizeof("Forked thread 1\n"), -1, -1);
	while(1)
	{
		AcquireLock(testLock0);
		Print("Thread1 acquired lock\n",sizeof("Thread1 acquired lock\n"),-1,-1);
		if(testMonitor)
		{
			Print("testMonitor has been set to true, signaling thread0 now\n",sizeof("testMonitor has been set to true, signaling thread0 now\n"),-1,-1); 
			SignalCondition(testCV, testLock0);
			ReleaseLock(testLock0);
			break;
		}
		ReleaseLock(testLock0);
	}
	Exit(1);
}

int main()
{
	testLock0 = CreateLock("testLock0",9);
	testCV = CreateCondition("testCV", 6);
	testLock1 = CreateLock("testLock1", 9);
	testMonitor = 0;
	Fork(thread0, "thread0", 7);
	Fork(thread1, "thread1", 7);
	
	Exit(1);
	return 1;
}
